package com.bnz.search.config;

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import co.elastic.clients.transport.ElasticsearchTransport;
import co.elastic.clients.transport.rest_client.RestClientTransport;
import lombok.Data;
import org.apache.http.HttpHost;

import org.elasticsearch.client.RestClient;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@Data
@ConfigurationProperties(prefix = "elasticsearch")
public class ElasticsearchConfig {

   private String host;
   private int port;
   @Bean
    public ElasticsearchClient elasticsearchClient(){
       // Create the low-level client
       RestClient restClient = RestClient.builder(
               new HttpHost(host, port)).build();

// Create the transport with a Jackson mapper
       ElasticsearchTransport transport = new RestClientTransport(
               restClient, new JacksonJsonpMapper());

// And create the API client
       ElasticsearchClient client = new ElasticsearchClient(transport);

       return client;
   }
}
